<template>
    <ContentWrap>
        <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-15px" label-width="85px" @submit.native.prevent>
            <el-form-item label="轮播图名称" prop="name">
                <el-input v-model="queryParams.name" class="!w-240px" clearable placeholder="请输入轮播图名称" @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item>
                <el-button @click="handleQuery">
                    <Icon class="mr-5px" icon="ep:search"/>
                    搜索
                </el-button>
                <el-button @click="resetQuery">
                    <Icon class="mr-5px" icon="ep:refresh"/>
                    重置
                </el-button>
                <el-button v-hasPermi="['yxt:banner:create']" plain type="primary" @click="openForm('create')">
                    <Icon class="mr-5px" icon="ep:plus"/>
                    新增
                </el-button>
            </el-form-item>
        </el-form>
    </ContentWrap>

    <ContentWrap>
        <el-table v-loading="loading" :data="list">
            <el-table-column label="轮播图名称" prop="name" min-width="120"/>
            <el-table-column label="轮播顺序" prop="sort"/>
            <el-table-column label="图片" prop="picture" width="50">
                <template #default="scope">
                    <el-image :src="scope.row.picture"/>
                </template>
            </el-table-column>
            <el-table-column label="跳转类型" prop="type">
                <template #default="scope">
                    <dict-tag :type="DICT_TYPE.YXT_BANNER_TYPE" :value="scope.row.type"/>
                </template>
            </el-table-column>
            <el-table-column label="跳转地址" prop="redirect" min-width="200" show-overflow-tooltip/>
            <el-table-column label="状态" prop="status">
                <template #default="scope">
                    <dict-tag :type="DICT_TYPE.YXT_BANNER_STATUS" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <el-table-column :formatter="dateFormatter" align="center" label="上传时间" prop="createTime" width="160"/>
            <el-table-column :width="150" align="center" label="操作" fixed="right">
                <template #default="scope">
                    <el-button v-hasPermi="['yxt:banner:update']" link type="primary" @click="openForm('update', scope.row.id)">
                        编辑
                    </el-button>
                    <el-button v-hasPermi="['yxt:banner:delete']" link type="danger" @click="handleDelete(scope.row.id)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </ContentWrap>

    <BannerForm ref="formRef" @success="getList"/>
</template>

<script lang="ts" setup>
import {DICT_TYPE} from "@/utils/dict";
import BannerForm from "@/views/yxt/banner/BannerForm.vue";
import {useBannerApi} from "@/api/yxt/banner";
import {dateFormatter} from "@/utils/formatTime";
import {useConfirm, useMessage} from "@/components/Notice";

defineOptions({name: 'YxtBanner'})

const bannerApi = useBannerApi();
const {t} = useI18n()

const loading = ref(true);
const list = ref([])
const queryParams = reactive({
    name: '',
})

const queryFormRef = ref()
const getList = async () => {
    loading.value = true
    try {
        list.value = await bannerApi.getList(queryParams)
    } finally {
        loading.value = false
    }
}

const handleQuery = () => {
    getList()
}

const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
}

const formRef = ref()
const openForm = (type: string, id?: number) => {
    formRef.value.open(type, id)
}

const handleDelete = (id: number) => {
    useConfirm(t("common.delMessage"), async () => {
        await bannerApi.deleteObj(id);
        getList().finally(() => useMessage(t('common.delSuccess')));
    });
}

onMounted(() => {
    getList()
})
</script>
